package com.auemall.service.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auemall.constant.ApplicationConstant;
import com.auemall.dao.SysUserPOMapper;
import com.auemall.po.SysUserPO;
import com.auemall.service.UserService;
import com.auemall.utils.StringUtils;
import com.auemall.vo.BaseRespVO;
import com.auemall.vo.UserVO;

@Service
public class UserServiceImpl implements UserService {

	private Logger logger = Logger.getLogger(UserServiceImpl.class);

	/**
	 * 下面注入本系统dao
	 */
	@Autowired
	private SysUserPOMapper sysUserPOMapper; // 用户信息表

	@Override
	public BaseRespVO addUser(UserVO req) throws Exception {
		BaseRespVO respVO = new BaseRespVO();
		if (null == req) {
			respVO.setResult(ApplicationConstant.RESULT_FALTURE);
			respVO.setMsg("请求参数为空！");
			logger.error(respVO.getMsg());
			return respVO;
		}

		if (StringUtils.isBlank(req.getAccountName())) {
			respVO.setResult(ApplicationConstant.RESULT_FALTURE);
			respVO.setMsg("账户为空！");
			logger.error(respVO.getMsg());
			return respVO;
		}

		if (StringUtils.isBlank(req.getMobile())) {
			respVO.setResult(ApplicationConstant.RESULT_FALTURE);
			respVO.setMsg("手机号为空！");
			logger.error(respVO.getMsg());
			return respVO;
		}

		if (StringUtils.isBlank(req.getPwd())) {
			respVO.setResult(ApplicationConstant.RESULT_FALTURE);
			respVO.setMsg("密码为空！");
			logger.error(respVO.getMsg());
			return respVO;
		}

		if (StringUtils.isBlank(req.getRealName())) {
			respVO.setResult(ApplicationConstant.RESULT_FALTURE);
			respVO.setMsg("真实姓名为空！");
			logger.error(respVO.getMsg());
			return respVO;
		}
		
		//设置默认值
		req.setCreatetime(new Date());
		req.setLocked(0);

		SysUserPO record = new SysUserPO();
		BeanUtils.copyProperties(req, record);

		sysUserPOMapper.insertSelective(record);
		respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
		respVO.setMsg("增加用户成功");
		return respVO;
	}

	@Override
	public BaseRespVO updateUser(UserVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(req.getId()==null||req.getId()==0){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[ID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        SysUserPO record = new SysUserPO();
        BeanUtils.copyProperties(req, record);
        record.setId(req.getId());

        sysUserPOMapper.updateByPrimaryKeySelective(record);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("修改用户信息成功");
        return respVO;
	}

	@Override
	public BaseRespVO deleteUser(UserVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(req.getId()==null||req.getId()==0){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[ID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        sysUserPOMapper.deleteByPrimaryKey(req.getId());
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("删除用户成功");
        return respVO;
	}

}
